const express=require('express');
const fs=require('fs');
let router_admin_delete=express.Router();

router_admin_delete.get('/',(req,res)=>{
    let table=req.baseUrl.split('/')[2];
    console.log(table)
    let ID=req.query['id'];

    let aID=ID.split(',');

    let b_error=false;
    aID.forEach(item=>{
        if(!/^(\d|[a-f]){32}$/.test(item)){
            b_error=true;
        }
    })

    if(b_error){
        res.sendStatus(400);
    }else{
        let id_index=0;
        next();
        function next(){
            let ID=aID[id_index++];
            let sql=`SELECT * FROM ${table}_table WHERE ID='${ID}'`;
            req.db.query(sql,(err,data)=>{
                if(err){
                    console.log(err)
                    console.log(1)
                    res.sendStatus(500);
                }else if(data.length==0){
                    res.sendStatus(404);
                }else{
                    // main_img_real_path
                    // img_real_paths
                    // property_img_real_paths
                    // 1.删除本地路径的图片
                    let deleteArr=[];
                    if(data[0].main_img_real_path){
                        deleteArr.push(data[0].main_img_real_path);
                    }
                    if(data[0].img_real_paths){
                        data[0].img_real_paths.split(',').forEach(item => {
                            deleteArr.push(item)
                        });
                    }
                    if(data[0].property_img_real_paths){
                        data[0].property_img_real_paths.split(',').forEach(item => {
                            deleteArr.push(item)
                        });
                    }
                    function deleteFromDB(){
                        req.db.query(`DELETE FROM ${table}_table WHERE ID='${ID}'`,err=>{
                            if(err){
                                console.log(err)
                                res.sendStatus(500)
                            }else{
                                if(id_index<aID.length){
                                    next();
                                }else{
                                    res.redirect(`/admin/${table}`);   
                                }
                            }
                        })
                    }
                    if (deleteArr.length>0){
                        let i=0;
                        removeImgReal()
                        function removeImgReal(){
                            fs.unlink(deleteArr[i],err=>{
                                if(err){
                                    console.log(err)
                                    res.sendStatus(500)
                                }else{
                                    i++;
                                    if(i>=deleteArr.length){
                                        i=0;
                                        // 2.删除数据本身
                                        deleteFromDB();
                                        
                                    }else{
                                        removeImgReal();
                                    }
                                }
                            })
            
                        }
                    }else{
                        deleteFromDB();
                    }
                    
                } 
            })
        }
        
    }
})


module.exports=router_admin_delete;